package com.threeti.mecool.core.domain.model.acl;

import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;
import com.threeti.mecool.framework.util.Lists;

import javax.persistence.*;
import java.util.List;

/**
 * 操作权限角色
 *
 * @author Jay Meng
 */
@Entity
@Table(name = "DM_PERMISSIONROLE")
public class PermissionRole extends DefaultDomainEntity {
    @Column(nullable = false, unique = true)
    private String name;

    @ElementCollection//(fetch = FetchType.EAGER)
    @CollectionTable(name = "DM_PERMISSIONROLE_PERM_ENTRY")
    @Column(name = "PERMISSION_ENTRY", length = 50)
    private List<String> permissionEntries = Lists.newArrayList();

    @ElementCollection
    @CollectionTable(name = "DM_PERMROLE_ADMIN_DIV_ADCODE")
    @Column(name = "MANAGED_ADMINDIV_ADCODE", length = 50)
    private List<String> managedAdminDivisionCodeEntries = Lists.newArrayList();

    @ElementCollection
    @CollectionTable(name = "DM_PERMISSIONROLE_ORG_CODE")
    @Column(name = "MANAGED_ORG_CODE", length = 50)
    private List<String> managedOrgCodeEntries = Lists.newArrayList();

    private boolean isAdvanced = false;
    
    
    private Integer useADFilter;

    public Integer getUseADFilter() {
        return useADFilter == null ? 0 : useADFilter;
    }

    public void setUseADFilter(Integer useADControl) {
        this.useADFilter = useADControl == null ? 0 : useADControl;
    }

    public PermissionRole(String name) {
        super();
        this.name = name;
    }

    public PermissionRole(String name, boolean isAdvanced) {
        super();
        this.name = name;
        this.isAdvanced = isAdvanced;
    }

    /**
     * 取得操作权限角色名
     */
    public String getName() {
        return name;
    }

    /**
     * 是否高级管理者权限
     */
    public boolean isAdvanced() {
        return isAdvanced;
    }

    /**
     * 取得角色的系统操作权限集合
     */
    public List<String> getPermissionEntries() {
        return permissionEntries;
    }

    /**
     * 取得角色所管辖或所能查阅的行政区划(编码)集合
     */
    public List<String> getManagedAdminDivisionCodeEntries() {
        return managedAdminDivisionCodeEntries;
    }

    /**
     * 取得角色所管辖或所能查阅的组织机构(编码)集合
     */
    public List<String> getManagedOrgCodeEntries() {
        return managedOrgCodeEntries;
    }

    public static PermissionRole valueOf(String name) {
        return new PermissionRole(name);
    }

    public static PermissionRole valueOf(String name, boolean isAdvanced) {
        return new PermissionRole(name, isAdvanced);
    }

    /**
     * 添加特定的操作权限(操作权限以逗号分隔，如"100001,m_admin_usracl,<perm mask>")
     */
    public PermissionRole addPermissionEntry(String permissionEntry) {
        permissionEntries.add(permissionEntry);
        return this;
    }

    /**
     * 添加特定角色的行政区划管理权限，行政区划管理权限主要应用于报表查看的数据筛选
     * <p/>
     * 角色的行政区划管理权限只支持到AdminDivisionLevelDeep.CITY级深度
     *
     * @see com.threeti.mecool.legacy.domain.model.basicdata.AdminDivisionLevelDeep
     */
    public PermissionRole addManagedAdminDivisionEntry(String adminDivAdCode) {
        managedAdminDivisionCodeEntries.add(adminDivAdCode);
        return this;
    }

    /**
     * 添加角色所管辖或所能查阅的组织机构(编码)
     */
    public PermissionRole addManagedOrgCode(String orgCode) {
        managedOrgCodeEntries.add(orgCode);
        return this;
    }

    @Override
    public String toString() {
        return "PermissionRole [name=" + name + ", isAdvanced=" + isAdvanced + "]";
    }

    public PermissionRole() {
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPermissionEntries(List<String> permissionEntries) {
        this.permissionEntries = permissionEntries;
    }

    public void setManagedAdminDivisionCodeEntries(List<String> managedAdminDivisionCodeEntries) {
        this.managedAdminDivisionCodeEntries = managedAdminDivisionCodeEntries;
    }

    public void setManagedOrgCodeEntries(List<String> managedOrgCodeEntries) {
        this.managedOrgCodeEntries = managedOrgCodeEntries;
    }

    public void setAdvanced(boolean isAdvanced) {
        this.isAdvanced = isAdvanced;
    }

}
